package com.arkopp.partyscanner;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.util.Log;


public class XMLHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	private boolean inResponse = false;
	private boolean inStatus = false;
	private boolean inFirstName = false;
	private boolean inLastName = false;
	private boolean inEmail = false;
	private boolean inImage = false;
	
	
	private String tmpStatus;
	private String tmpFirstName;
	private String tmpLastName;
	private String tmpEmail;
	private String tmpImage;

	private Response response;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public Response getParsedData() {
		return response;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		// Nothing to do
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("response")) {
			inResponse = true;
		} else if (localName.equals("status")) {
			inStatus = true;
		} else if (localName.equals("firstName")) {
			inFirstName = true;
		}else if (localName.equals("lastName")) {
			inLastName = true;
		}else if (localName.equals("email")) {
			inEmail = true;
		}else if (localName.equals("image")) {
			inImage = true;
		}
	}

	/** Gets be called on closing tags like:
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("response")) {
			inResponse = false;
			response = new Response(tmpStatus, tmpFirstName, tmpLastName, tmpEmail, tmpImage);
		}else if (localName.equals("firstName")) {
			inFirstName = false;
		}else if (localName.equals("lastName")) {
			inLastName = false;
		}else if (localName.equals("email")) {
			inEmail = false;
		}else if (localName.equals("image")) {
			inImage = false;
		}else if (localName.equals("status")) {
			inStatus = false;
		}
	}

	/** Gets be called on the following structure:
	 * <tag>characters</tag> */
	@Override
	public void characters(char ch[], int start, int length) {
		if (inFirstName) {
			tmpFirstName = new String(ch, start, length);
		} else if (inLastName) {
			tmpLastName = new String(ch, start, length);
		} else if (inStatus) {
			tmpStatus = new String(ch, start, length);
		} else if (inEmail) {
			tmpEmail = new String(ch, start, length);
		} else if (inImage) {
			tmpImage = new String(ch, start, length);
			Log.w("PSCAN", tmpImage);
		}
	}
}